﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models.Airplane;
using System.Net;

namespace G15_Agent.BUS
{
    public class BUS_Airplane
    {
        //public Service.Airplane_G03.Service1SoapClient serviceG03 { get; set; }
        public Service.Airplane_G03.Service1 serviceG03 { get; set; }

        public BUS_Airplane()
        {
            //serviceG03 = new Service.Airplane_G03.Service1SoapClient("Service1Soap1");
            serviceG03 = new Service.Airplane_G03.Service1();
        }

        #region WSAirplane0 - Đăng nhập

        public bool DangNhap_G03(string userName, string password)
        {
            try
            {
                CookieContainer cookieJar = new CookieContainer();
                serviceG03.CookieContainer = cookieJar;
                return serviceG03.DangNhap(userName, password);
            }
            catch (System.Exception ex)
            {
                string logError = ex.ToString();
            }
            return false;
        }

        #endregion

        #region WSAirllane1 - Lâý danh sách sân bay đi

        public List<SanBay> LayDanhSachSanBayDi_G03()
        {
            List<SanBay> result = new List<SanBay>();
            try
            {
                Service.Airplane_G03.SanBay[] dsSanBay = serviceG03.TimSanBayDi();
                foreach (Service.Airplane_G03.SanBay serverItem in dsSanBay)
                {
                    SanBay clientItem = new SanBay();
                    clientItem.maSanBay = serverItem.MaSanBay;
                    clientItem.tenSanBay = serverItem.TenSanBay;
                    clientItem.tenQuocGia = serverItem.QuocGia;
                    clientItem.tenKhuVuc = serverItem.TenThanhPho;

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region WSAirllane2 - Lâý danh sách sân bay đến

        public List<SanBay> LayDanhSachSanBayDen_G03()
        {
            List<SanBay> result = new List<SanBay>();
            try
            {
                Service.Airplane_G03.SanBay[] dsSanBay = serviceG03.TimSanBayDen();
                foreach (Service.Airplane_G03.SanBay serverItem in dsSanBay)
                {
                    SanBay clientItem = new SanBay();
                    clientItem.maSanBay = serverItem.MaSanBay;
                    clientItem.tenSanBay = serverItem.TenSanBay;
                    clientItem.tenQuocGia = serverItem.QuocGia;
                    clientItem.tenKhuVuc = serverItem.TenThanhPho;

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region WSAirllane3 - Lâý danh sách loại vé

        public List<LoaiVe> LayDanhSachLoaie_G03()
        {
            List<LoaiVe> result = new List<LoaiVe>();
            try
            {
                Service.Airplane_G03.LoaiVe[] dsSanBay = serviceG03.TimLoaiVe();
                foreach (Service.Airplane_G03.LoaiVe serverItem in dsSanBay)
                {
                    LoaiVe clientItem = new LoaiVe();
                    clientItem.maLoaiVe = serverItem.MaLoai;
                    clientItem.tenLoaiVe = serverItem.TenLoai;
                    clientItem.thoiHanSuDung = serverItem.ThoiHanSuDung;
                    clientItem.donViTien = serverItem.DonViTien;
                    clientItem.doiLichTrinh = serverItem.DuocPhepDoiLichTrinh;
                    clientItem.huyVe = serverItem.DuocPhepHuy;
                    clientItem.nangHang = serverItem.DuocPhepNangHang;
                    clientItem.heSoCongDam = serverItem.HeSoCongDam;
                    clientItem.phiDoiLichTrinhSauKH = serverItem.PhiDoiLichTrinhSauNgayKhoiHanh;
                    clientItem.phiDoiLichTrinhTruocKH = serverItem.PhiDoiLichTrinhTruocNgayKhoiHanh;
                    clientItem.phiHuySauKH = serverItem.PhiHuySauNgayKhoiHanh;
                    clientItem.phiHuyTruocKH = serverItem.PhiHuyTruocNgayKhoiHanh;
                    clientItem.phiNangHangSauKH = serverItem.PhiNangHangSauNgayKhoiHanh;
                    clientItem.phiNangHangTruocKH = serverItem.PhiNangHangTruocNgayKhoiHanh;

                    result.Add(clientItem);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region WSAirPlane4 - Lâý giá vé
        //loaiCHuyenBay: khu hoi/1 chieu
        public double LayGiaVe_G03(string maLoaiVe, SanBay diemDi, SanBay diemDen, string loaiChuyenBay, DateTime ngayDi, string gioBay)
        {
            double result = 0;
            try
            {
                result = serviceG03.TimGiaVe1(maLoaiVe, diemDi.maSanBay, diemDen.maSanBay, loaiChuyenBay, ngayDi, gioBay);
            }
            catch (System.Exception ex)
            {
                string logError = ex.ToString();
            }

            return result;
        }

        #endregion

        #region WSAirplane7 - Đặt vé máy bay cho đoàn

        public HoaDonMayBay DatVeMayBay_G03(string maLoaiVe, DateTime ngayGioDi, DateTime ngayGioVe,
            string loaiChuyenBay, string maSanBayDi, string maSanBayDen, List<KhachHang> dsKhachHang)
        {
            HoaDonMayBay result = new HoaDonMayBay();
            G15_Agent.Service.Airplane_G03.KhachHang[] dsKhachHangServer = new G15_Agent.Service.Airplane_G03.KhachHang[dsKhachHang.Count];
            for (int i = 0; i < dsKhachHang.Count; i++)
            {
                dsKhachHangServer[i].Email = dsKhachHang[i].email;
                dsKhachHangServer[i].GioiTinh  = dsKhachHang[i].gioiTinh;
                dsKhachHangServer[i].HoTenKhachHang = dsKhachHang[i].hoTenKhachHang;
                dsKhachHangServer[i].LoaiDoiTuong = dsKhachHang[i].loaiDoiTuong;
                dsKhachHangServer[i].LoaiGiayTo = dsKhachHang[i].loaiGiayTo;
                dsKhachHangServer[i].NgayHetHan = dsKhachHang[i].ngayHetHan;
                dsKhachHangServer[i].NgaySinh = dsKhachHang[i].ngaySinh;
                dsKhachHangServer[i].NuocPhatHanh = dsKhachHang[i].nuocPhatHanh;
                dsKhachHangServer[i].QuocTich = dsKhachHang[i].quocTich;
                dsKhachHangServer[i].SoDienThoaiBan = dsKhachHang[i].soDienThoaiBan;
                dsKhachHangServer[i].SoDienThoaiDiDong = dsKhachHang[i].soDienThoaiDiDong;
                dsKhachHangServer[i].SoThe = dsKhachHang[i].soThe;
            }

            try
            {
                G15_Agent.Service.Airplane_G03.HoaDonMayBay itemServer = serviceG03.DatVeMayBay(maLoaiVe, ngayGioDi, ngayGioVe, loaiChuyenBay, maSanBayDi, maSanBayDen, dsKhachHangServer);                
                result.iD_hoaDon = itemServer.ID_hoaDon1;
                result.loaiChuyenBay = itemServer.LoaiChuyenBay;
                result.loaiVe = itemServer.LoaiVe;
                result.ngayDat = itemServer.NgayDat;
                result.taiKhoan = itemServer.TaiKhoan;
                result.tongTien = itemServer.TongTien;
                result.listVe = new VeMayBay[itemServer.ListVe.Count()];
                for (int i = 0; i < itemServer.ListVe.Count(); i++)
                {
                    result.listVe[i].giaVe = itemServer.ListVe[i].GiaVe;
                    result.listVe[i].hangVe = itemServer.ListVe[i].HangVe;
                    result.listVe[i].id_Ve = itemServer.ListVe[i].ID_ve1;
                    result.listVe[i].soHieuMayBay = itemServer.ListVe[i].SoHieuMayBay;
                    result.listVe[i].tenKhachHang = itemServer.ListVe[i].TenKhachHang;
                    result.listVe[i].thue = itemServer.ListVe[i].Thue;
                    result.listVe[i].tongTien = itemServer.ListVe[i].TongTien;
                    result.listVe[i].listChiTiet = new ChiTietChangBayChuyenBay[itemServer.ListVe[i].ListChiTiet.Count()];
                    for (int j = 0; j < itemServer.ListVe[i].ListChiTiet.Count(); j++)
                    {
                        result.listVe[i].listChiTiet[j].gioDen = itemServer.ListVe[i].ListChiTiet[j].GioDen;
                        result.listVe[i].listChiTiet[j].gioKhoiHanh = itemServer.ListVe[i].ListChiTiet[j].GioKhoiHanh;
                        result.listVe[i].listChiTiet[j].ngayDen = itemServer.ListVe[i].ListChiTiet[j].NgayDen;
                        result.listVe[i].listChiTiet[j].ngayKhoiHanh = itemServer.ListVe[i].ListChiTiet[j].NgayKhoiHanh;
                        result.listVe[i].listChiTiet[j].ngayTrongTuanDen = itemServer.ListVe[i].ListChiTiet[j].NgayTrongTuanDen;
                        result.listVe[i].listChiTiet[j].ngayTrongTuanKhoiHanh = itemServer.ListVe[i].ListChiTiet[j].NgayTrongTuanKhoiHanh;
                        result.listVe[i].listChiTiet[j].tenThanhPhoDen = itemServer.ListVe[i].ListChiTiet[j].TenThanhPhoDen;
                        result.listVe[i].listChiTiet[j].tenThanhPhoDi = itemServer.ListVe[i].ListChiTiet[j].TenThanhPhoDi;
                        result.listVe[i].listChiTiet[j].thoiHanVe = itemServer.ListVe[i].ListChiTiet[j].ThoiHanVe;
                        result.listVe[i].listChiTiet[j].tongThoiGianBay = itemServer.ListVe[i].ListChiTiet[j].TongThoiGianBay;
                        result.listVe[i].listChiTiet[j].viTri = itemServer.ListVe[i].ListChiTiet[j].ViTri;
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            
            return result;
        }

        #endregion

        #region WSAirplane9 - Tìm kiếm chuyến bay
        //loaiChuyenBay: khu hoi/ 1 chieu       

        public List<ChuyenBay> TimChuyenBay_G03(int soTreEm, int soNguoiLon, string loaiVe,
           DateTime ngayDi, DateTime ngayVe, string loaiChuyenBay, string diemDi, string diemDen)
        {
            List<ChuyenBay> result = new List<ChuyenBay>();
            try
            {
                Service.Airplane_G03.ChuyenBay1[] dsChuyenBay = serviceG03.TimKiemChuyenBay1(soNguoiLon, loaiVe, ngayDi, ngayVe, loaiChuyenBay, diemDi, diemDen);
                foreach (Service.Airplane_G03.ChuyenBay1 serverItem in dsChuyenBay)
                {
                    ChuyenBay clientItem = new ChuyenBay();
                    clientItem.maChuyenBay = "";
                    clientItem.loaiChuyen = serverItem.LoaiChuyenBay.Trim();
                    clientItem.maSanBayDen = serverItem.MaSanBayDen.Trim();
                    clientItem.maSanBayDi = serverItem.MaSanBayDi.Trim();
                    clientItem.ngayKhoiHanh = serverItem.NgayDi.ToString();
                    clientItem.ngayDen = "";
                    clientItem.ngayVe = serverItem.NgayDen.Trim();
                    clientItem.giaVe = serverItem.GiaVe;
                    clientItem.donViTien = serverItem.DonViTien;
                    clientItem.gioDi = serverItem.GioDi;
                    clientItem.gioDen = serverItem.GioDen;

                    result.Add(clientItem);
                }

            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

    }
}